//
//  HCHomeBottomView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/17.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCHomeBottomView.h"

#import "HCHomeBottomCollectionViewCell.h"
#import "HCGoodsListModel.h"

@interface HCHomeBottomView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
    HCGoodsListModel *models_;
}

@property (nonatomic, strong) UICollectionView *collectionView_;

@end

@implementation HCHomeBottomView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)setCollectionScolled:(BOOL)collectionScolled {
    self.collectionView_.scrollEnabled = collectionScolled;
}

-(void)initializeSubViews:(CGRect)frame {
    
    //初始化视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, YBKAdaptiveH(26))];
    view.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    [self addSubview:view];
    self.backgroundColor = [UIColor whiteColor];
    
    UIImage *im = [UIImage imageNamed:@"happy_home_bestsgoods"];
    
    UIImageView *topPrompt = [[UIImageView alloc] initWithFrame:CGRectMake(0, view.frame.size.height - im.size.height*0.85, im.size.width*0.85, im.size.height*0.85)];
    topPrompt.image = im;
    [view addSubview:topPrompt];
//    topPrompt.center = view.center;
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 0;
    
    self.collectionView_ = [[UICollectionView alloc] initWithFrame:CGRectMake(0, view.frame.origin.y+view.frame.size.height, frame.size.width, 200) collectionViewLayout:flow];
    self.collectionView_.delegate = self;
    self.collectionView_.dataSource = self;
    self.collectionView_.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
//    self.collectionView_.scrollEnabled = NO;
    [self addSubview:self.collectionView_];
    
    [self.collectionView_ registerNib:[UINib nibWithNibName:@"HCHomeBottomCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"homebottomview"];
    
    //注册通知
    [self.collectionView_ addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:@"frame"];
    
    
}

#pragma mark    UICollectionView代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (models_) {
        return models_.goods.count;
    }else{
        return 0;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if ([self.dataSource respondsToSelector:@selector(goodsViewClicked:)]&&self.dataSource) {
        [self.dataSource goodsViewClicked:indexPath];
    }
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((YBKWinFrame.size.width-30)/2.0, YBKAdaptiveH(225));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 10, 10, 10);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HCHomeBottomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homebottomview" forIndexPath:indexPath];
    if (models_) {
        HCGoodsListData *data = models_.goods[indexPath.row];
        cell.model = data;
    }
//    NSLog(@"cell刷新数据%ld ----- %ld",indexPath.section,indexPath.row);
    return cell;
}

#pragma mark 观察者的回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentSize"]) {
        CGSize size;
        [change[@"new"] getValue:&size];
        if ([NSStringFromClass(object_getClass(self.dataSource)) isEqualToString:@"HCScoreStoreViewController"]) {
            self.collectionView_.frame = CGRectMake(self.collectionView_.frame.origin.x, self.collectionView_.frame.origin.y, self.collectionView_.frame.size.width, self.frame.size.height - YBKAdaptiveH(16));
            CGFloat height = size.height + YBKAdaptiveH(16)*2.0;
            [self.dataSource homeBottomViewSize:height bottomView:self];
        }else if ([NSStringFromClass(object_getClass(self.dataSource)) isEqualToString:@"HCHomeViewController"]){
            self.collectionView_.frame = CGRectMake(self.collectionView_.frame.origin.x, self.collectionView_.frame.origin.y, self.collectionView_.frame.size.width, size.height);
            CGFloat height = size.height + YBKAdaptiveH(16)*2.0;
            [self.dataSource homeBottomViewSize:height bottomView:self];
        }
        

    }
}

-(void)dealloc {
    [self.collectionView_ removeObserver:self forKeyPath:@"contentSize"];
}

-(void)setModel_goods:(id)model_goods {
    models_ = (HCGoodsListModel *)model_goods;
    [self.collectionView_ reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
